
package com.netease.xone.adapter;

import java.util.ArrayList;
import java.util.List;

import protocol.XoneCallBack;
import protocol.XoneService;
import protocol.meta.Source;
import protocol.meta.Subject;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.netease.xone.R;
import com.netease.xone.itemview.RenderSourceView;
import com.netease.xone.itemview.RenderSubjectView;

public class SubjectAndSourceAdapter extends BaseExpandableListAdapter {

    // 将groupType分为三种类型
    public static final int GROUP_TYPE_SUBJECT = 0;
    public static final int GROUP_TYPE_SOURCE = 1;
    public static final int GROUP_TYPE_TITLE = 2;

    private List<Subject> mSubjectList;
    private List<Source> mSourceList;
    private String mSearchString;
    private Context mContext;

    public SubjectAndSourceAdapter(Context context) {
        mContext = context;
        mSubjectList = new ArrayList<Subject>();
        mSourceList = new ArrayList<Source>();
        XoneService.getInstance().addListener(mCallBack);
    }

    public void setData(String searchString, List<Subject> subjectList, List<Source> sourceList) {
        mSearchString = searchString;
        mSubjectList.addAll(subjectList);
        mSourceList.addAll(sourceList);
        notifyDataSetChanged();
    }

    @Override
    public Source getChild(int groupPosition, int childPosition) {
        if (getGroupType(groupPosition) == GROUP_TYPE_SUBJECT) {
            return mSubjectList.get(getSubjectListPosition(groupPosition)).sourceList
                    .get(childPosition);
        } else {
            return null;
        }
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {
        if (convertView == null) {
            View v = View.inflate(mContext, R.layout.view_source_item, null);
            RenderSourceView render = new RenderSourceView(v);
            v.setTag(render);
            convertView = v;
        }
        RenderSourceView render = (RenderSourceView)convertView.getTag();
        render.renderView(mContext, getChild(groupPosition, childPosition), childPosition == 0,
                isLastChild);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (getGroupType(groupPosition) == GROUP_TYPE_SUBJECT) {
            int childCount = mSubjectList.get(getSubjectListPosition(groupPosition)).sourceList == null ? 0
                    : mSubjectList.get(getSubjectListPosition(groupPosition)).sourceList.size();
            return childCount;
        } else {
            return 0;
        }
    }

    @Override
    public Object getGroup(int groupPosition) {
        if (getGroupType(groupPosition) == GROUP_TYPE_SUBJECT) {
            return mSubjectList.get(getSubjectListPosition(groupPosition));
        } else if (getGroupType(groupPosition) == GROUP_TYPE_SOURCE) {
            return mSourceList.get(getSourceListPosition(groupPosition));
        } else {
            return null;
        }
    }

    @Override
    public int getGroupTypeCount() {
        return 3;
    }

    /**
     * 获得SubjectList中的真实位置
     *
     * @param groupPosition
     * @return
     */
    private int getSubjectListPosition(int groupPosition) {
        return groupPosition - 1;
    }

    /**
     * 获得SourceList中的真实位置
     *
     * @param groupPosition
     * @return
     */
    private int getSourceListPosition(int groupPosition) {
        return groupPosition - mSubjectList.size() - 2;
    }

    @Override
    public int getGroupType(int groupPosition) {
        if (groupPosition == 0 || groupPosition == mSubjectList.size() + 1) {
            return GROUP_TYPE_TITLE;
        } else if (groupPosition <= mSubjectList.size()) {
            return GROUP_TYPE_SUBJECT;
        } else {
            return GROUP_TYPE_SOURCE;
        }
    }

    @Override
    public int getGroupCount() {
        return mSubjectList.size() + mSourceList.size() + 2;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
            ViewGroup parent) {
        if (convertView == null) {
            switch (getGroupType(groupPosition)) {
                case GROUP_TYPE_SUBJECT: {
                    View v = View.inflate(mContext, R.layout.view_expand_subject_item, null);
                    RenderSubjectView render = new RenderSubjectView(v);
                    v.setTag(render);
                    convertView = v;
                }
                    break;
                case GROUP_TYPE_SOURCE: {
                    View v = View.inflate(mContext, R.layout.view_source_item, null);
                    RenderSourceView render = new RenderSourceView(v);
                    v.setTag(render);
                    convertView = v;
                }
                    break;
                case GROUP_TYPE_TITLE: {
                    View v = View.inflate(mContext,
                            R.layout.view_classify_search_result_title_item, null);
                    TitleViewRender render = new TitleViewRender(v);
                    v.setTag(render);
                    convertView = v;
                }
                    break;
            }
        }
        switch (getGroupType(groupPosition)) {
            case GROUP_TYPE_SUBJECT: {
                RenderSubjectView render = (RenderSubjectView)convertView.getTag();
                render.renderView(mContext, (Subject)getGroup(groupPosition));
            }
                break;
            case GROUP_TYPE_SOURCE: {
                RenderSourceView render = (RenderSourceView)convertView.getTag();
                render.renderView(mContext, (Source)getGroup(groupPosition), false, false);
            }
                break;
            case GROUP_TYPE_TITLE: {
                TitleViewRender render = (TitleViewRender)convertView.getTag();
                if (groupPosition == 0) {
                    render.text1.setText(mContext.getString(R.string.searched_x_contains,
                            mSubjectList.size()));
                    render.text2.setText(mSearchString);
                    render.text3.setText(R.string.the_subject);
                } else {
                    render.text1.setText(mContext.getString(R.string.searched_x_contains,
                            mSourceList.size()));
                    render.text2.setText(mSearchString);
                    render.text3.setText(R.string.the_resource);
                }
            }
                break;
        }
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private class TitleViewRender {

        TextView text1;
        TextView text2;
        TextView text3;

        public TitleViewRender(View v) {
            text1 = (TextView)v.findViewById(R.id.search_result_title_searched_count);
            text2 = (TextView)v.findViewById(R.id.search_result_title_search_text);
            text3 = (TextView)v.findViewById(R.id.search_result_title_search_type);
        }
    }

    public void removeListener() {
        XoneService.getInstance().removeListener(mCallBack);
    }

    XoneCallBack mCallBack = new XoneCallBack() {

        @Override
        public void onSubscribe(int transactionId, int[] subjectIdAndSourceId) {
            // 有订阅信息，需要更新订阅状态
            int subjectId = subjectIdAndSourceId[0];
            int sourceId = subjectIdAndSourceId[1];
            // 更新主题列表订阅状态
            for (Subject subject : mSubjectList) {
                if (subject.sourceList == null || subject.sourceList.size() == 0)
                    continue;// 忽略没有资讯源的主题
                if (subjectId > 0) {
                    // subjectId > 0,表明是订阅全部
                    if (subjectId == subject.subjectId) {
                        // 更新该主题下的所有资讯源
                        for (Source source : subject.sourceList) {
                            source.isSubscribed = true;
                            source.subscribeCount++;
                            subject.subscribeCount++;
                            subject.mySubCount++;
                        }
                        notifyDataSetChanged();
                        break;// 找到之后就没必要继续查找了
                    }
                } else {
                    // 订阅单个资讯源
                    for (Source source : subject.sourceList) {
                        if (sourceId == source.sourceId) {
                            // 更新该资讯源
                            source.isSubscribed = true;
                            source.subscribeCount++;
                            subject.mySubCount++;
                            subject.subscribeCount++;
                            notifyDataSetChanged();
                            break;// 找到之后就没必要继续查找了
                        }
                    }
                }
            }
            // 更新资讯源列表订阅状态
            for (Source source : mSourceList) {
                if (sourceId > 0 && sourceId == source.sourceId) {
                    source.isSubscribed = true;
                    source.subscribeCount++;
                    notifyDataSetChanged();
                    break;// 找到之后就没必要继续查找了
                }
            }

        };

        @Override
        public void onUnsubscribe(int transactionId, int sourceId) {
            // 更新订阅状态
            for (Subject subject : mSubjectList) {
                if (subject.sourceList == null || subject.sourceList.size() == 0)
                    continue;// 忽略没有资讯源的主题
                // 取消订阅单个资讯源
                for (Source source : subject.sourceList) {
                    if (sourceId == source.sourceId) {
                        // 更新该资讯源
                        source.isSubscribed = false;
                        source.subscribeCount--;
                        subject.mySubCount--;
                        subject.subscribeCount--;
                        notifyDataSetChanged();
                        break;// 找到之后就没必要继续查找了
                    }
                }
            }

            // 更新资讯源列表订阅状态
            for (Source source : mSourceList) {
                if (sourceId > 0 && sourceId == source.sourceId) {
                    source.isSubscribed = false;
                    source.subscribeCount--;
                    notifyDataSetChanged();
                    break;// 找到之后就没必要继续查找了
                }
            }
        };
    };
}
